# game constants
from constants import *

# cocos componets used by this scene
from cocos.director import director
from cocos.scene import Scene
from cocos.layer import Layer, ColorLayer
from cocos.scenes.transitions import *
from cocos.actions import *
from cocos.sprite import Sprite
from cocos.text import Label

# pyglet  componets used by this scene
import pyglet
from pyglet import gl, font
from pyglet.window import key

import random

class BattleLayer(Layer):

    is_event_handler = True     #: enable pyglet's events

    def __init__( self, interface,planet ):
        # call the parent's constructor
        super(BattleLayer, self).__init__()

        # keep track of the interface
        self.interface = interface
        
        # the planet on which this battle takes place
        self.planet = planet
        
        # just a uniform color background
        self.add(ColorLayer(random.randint(0,255),40,40,200), z=-1)
        
        # add a label
        self.infoLabel = Label( "Battle Layer",
            font_size=12,
            multiline=True,
            height=100,
            width=360,
            x=10,
            y=SCREEN_HEIGHT - 20,
            anchor_x=font.Text.LEFT,
            anchor_y=font.Text.TOP )
        self.add( self.infoLabel )
        
        
    def on_enter(self):
        super( BattleLayer, self ).on_enter()   
        
        # shedule a function to update itself (the graphic) every frame
        self.schedule(self.update)      


    def on_exit(self):
        super( BattleLayer, self ).on_exit()   
        
        
    def update(self,dt, *args, **kwargs):
        infoText = "Battle Screen\n"
        if self.planet.attackingShip != None:
            infoText += "\n"+OWNER_NAMES[self.planet.attackingShip.owner] + " is attacking " + self.planet.name
            infoText += "\nAttacking soldiers: " + str(len(self.planet.attackingShip.soldiers))
            infoText += "\nDefending soldiers: " + str(len(self.planet.soldiers))
            infoText += "\nPress W to win or E to lose"
            
        self.infoLabel.element.text = infoText
        


    def on_key_press( self, k , m ):
        #print "Code of pressed key: " + str(k)
        if k == key.W:
            # win the current battle
            if self.planet.inBattle == True:
                self.planet.solveBattle(OWNER_PLAYER) 
                self.unschedule(self.update)   
                director.pop() 
        if k == key.E:
            # lose the current battle
            if self.planet.inBattle == True:
                self.planet.solveBattle(OWNER_ENEMY)   
                director.pop()
                
        if k == key.ESCAPE:
            # this should prevent ESC to pop this scene
            return pyglet.event.EVENT_HANDLED
            
               

# construct the scene here and return
# instead of doing this in the graphic interface (for example self.genericScene = Scene (GenericLayer()) )
# this is because here one can define more complicated scenes, independent of the interface
def BattleScene(interface,planet):
    # in this generic example, build a scene composed by only one layer
    return Scene( BattleLayer(interface,planet) )




